package com.action.springboot.hcnet;


import com.sun.jna.Native;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import static com.action.springboot.hcnet.HCNetSDK.*;


@Slf4j
@RestController
public class HkController {

	@Autowired
	private ReInfoMachineMapper infoMachineMapper;

	public static HCNetSDK hCNetSDK = null;
	static int lUserID = -1;//用户句柄

	public static int lAlarmHandle = -1;
	public static int lListenHandle = -1;

	static int iCharEncodeType = 0;//设备字符集

	static String check = "X";

	static {

		CreateSDKInstance();

		ifLinux();

		hCNetSDK.NET_DVR_Init();

	}


	@RequestMapping("/mylogin")
	public String myLogin(
			@RequestParam String code
	){

		if (lUserID > -1 ){
			Logout(lUserID);
			lUserID = -1;
			check = "X";
		}

		ReInfoMachine reInfoMachine = infoMachineMapper.selectReInfoMachineByCode(code);
		if (reInfoMachine == null) {
			return "没有该设备";
		}

		lUserID = Login(reInfoMachine.getIp(), reInfoMachine.getUser(), reInfoMachine.getPassword(), Short.parseShort(reInfoMachine.getPort()));//登陆

		if (lUserID == -1) {
			return "登录失败";
		}

		check = code;

		return "登录成功";
	}

	@RequestMapping("/direction")
	public String direction(
			@RequestParam String lrud , @RequestParam String code
	){

		if (lUserID == -1 ){
			return "未登录,无权限访问";
		}
		if ( !code.equals(check) ) {
			return "验证失败";
		}

		int iChannel = 1;//设备通道号
		int dwPTZCommand = 0;

		if ( lrud.equals("left")){
			dwPTZCommand = PAN_LEFT;	//云台左转
		}
		else if ( lrud.equals("right")){
			dwPTZCommand = PAN_RIGHT;
		}
		else if ( lrud.equals("up")){
			dwPTZCommand = TILT_UP;
		}
		else if ( lrud.equals("down")){
			dwPTZCommand = TILT_DOWN;
		}else {
			return "参数错误";
		}


		int dwStop = 0; //开始转动
		hCNetSDK.NET_DVR_PTZControl_Other(lUserID, iChannel, dwPTZCommand, dwStop);

		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		dwStop = 1; //停止转动
		hCNetSDK.NET_DVR_PTZControl_Other(lUserID, iChannel, dwPTZCommand, dwStop);

		return "结束";
	}

	@RequestMapping("/mylogout")
	public String myLogout(){

		Logout(lUserID);
		lUserID = -1;
		check = "X";
		return "结束";
	}



	/**
	 * 设备登录
	 * @param ipadress IP地址
	 * @param user  用户名
	 * @param psw  密码
	 * @param port 端口，默认8000
	 */
	public static int Login(String ipadress, String user, String psw, short port) {
		//注册
		HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
		String m_sDeviceIP = ipadress;//设备ip地址
		m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
		System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
		String m_sUsername = user;//设备用户名
		m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
		System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
		String m_sPassword = psw;//设备密码
		m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
		System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
		m_strLoginInfo.wPort = port; //sdk端口
		m_strLoginInfo.bUseAsynLogin = false; //是否异步登录：0- 否，1- 是
		m_strLoginInfo.write();
		HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
		int id = -1;
		id = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
		if (id == -1) {
			log.info("登录失败，错误码为" + hCNetSDK.NET_DVR_GetLastError());
			return -1;
		} else {
//			log.info("登录成功！");
			m_strDeviceInfo.read();
			iCharEncodeType = m_strDeviceInfo.byCharEncodeType;
			return id;

		}
	}



	//登出操作
	public static void Logout(int id){
		hCNetSDK.NET_DVR_Logout(id);
	}



	//根据不同操作系统选择不同的库文件和库路径
	private static boolean CreateSDKInstance() {
		if(hCNetSDK == null)
		{
			synchronized (HCNetSDK.class)
			{
				String strDllPath = "";
				try
				{
					//System.setProperty("jna.debug_load", "true");
					if(osSelect.isWindows())
						//win系统加载库路径
						strDllPath = System.getProperty("user.dir") + "\\hclib\\HCNetSDK.dll";
					else if(osSelect.isLinux())
						//Linux系统加载库路径
						strDllPath = System.getProperty("user.dir") + "/hclib/libhcnetsdk.so";
					hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
				}catch (Exception ex) {
					log.info("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
					return false;
				}
			}
		}
		return true;
	}

	private static void ifLinux(){

		//linux系统建议调用以下接口加载组件库
		if (osSelect.isLinux()) {
			HCNetSDK.BYTE_ARRAY ptrByteArray1 = new HCNetSDK.BYTE_ARRAY(256);
			HCNetSDK.BYTE_ARRAY ptrByteArray2 = new HCNetSDK.BYTE_ARRAY(256);
			//这里是库的绝对路径，请根据实际情况修改，注意改路径必须有访问权限
			String strPath1 = System.getProperty("user.dir") + "/hclib/libcrypto.so.1.1";
			String strPath2 = System.getProperty("user.dir") + "/hclib/libssl.so.1.1";

			System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
			ptrByteArray1.write();
			hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());

			System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
			ptrByteArray2.write();
			hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());

			String strPathCom = System.getProperty("user.dir") + "/hclib";
			HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
			System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
			struComPath.write();
			hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
		}

	}

}
